package com.test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description TODO
 * <p>
 * Copyright @ 2022 Shanghai Mise Co. Ltd.
 * All right reserved.
 * <p>
 * @Author LiuMingFu
 * @Date 2023/10/15 11:54
 */
public class Hello {
    public static void main(String[] args) {
        List<Object> list = new LinkedList<>();
        list.add("h");
        list.add("k");
        list.add("l");
        list.add(1);
//        String enumerationList = getEnumerationList(list, "");
//        System.out.println(enumerationList);
        System.out.println(convertList(list, String.class));
    }

    public static <T> String getEnumerationList(List<Object> parameterNames, T obj) {
        String result = "";
        for (Object parameterName : parameterNames) {
            T one = (T) parameterName;
            System.out.println(one);
            result += one + ",";
        }
        //去掉最后的逗号
        result = result.substring(0, result.lastIndexOf(","));
        return result;
    }


    /**
     * 将Object集合全部转为指定类型的集合
     *
     * @param objectList 目标集合
     * @param targetType 集合中元素需要强制转换的类型
     * @return
     * @author LiuMingFu
     * @date 2023/10/15
     */
    public static <T> List<T> convertList(List<Object> objectList, Class<T> targetType) {
        List<T> convertedList = new ArrayList<>();

        for (Object obj : objectList) {
            if (targetType.isInstance(obj)) {
                convertedList.add(targetType.cast(obj));
            } else {
                // Handle conversion failure or other logic
                // You can throw an exception, skip the object, or perform custom conversion logic
            }
        }

        return convertedList;
    }
}
